<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="/temp/default/js/jquery-3.1.0.min.js"></script>
    <script src="/temp/default/js/flexible.debug.js"></script>
    <script src="/temp/default/js/flexible_css.debug.js"></script>
    <link rel="stylesheet" href="/temp/default/css/index.css">
    <title>福利大抽奖</title>
</head>
<body>
    <div class="container">
        <p class="label">填写分享信息，吸引好友打开你分享的消息！</p>
        <input type="text" id="title" placeholder="分享标题例如:kaki送你一次免费抽奖活动" />
        <input type="text" id="desc" placeholder="分享的描述,至少6-12个字，越完善越有诱惑性" />
        <p class="label">试试上传一张照片作为分享分享的图标</p>
        <div class="upload">
            <form id="upfile" enctype="multipart/form-data" method="post">
                <input type="file" name="upload_name" id="upload" />
                <img id="img" src="/temp/default/image/upload.png" alt=""/>
                <input type="hidden" id="images" />
            </form>
        </div>
        <p class="infer">
            提示：<br/>
            1、输入分享标题和分享描述即可分享好友，图标不上传默认红包图标<br/>
            2、直接发送好友即可，也可以发送到朋友圈<br/>
            3、默认提供红包分享图标
        </p>
        <p><a href="index.php?action=panel_list&userid={$userid}">定位列表</a></p>
    </div>
</body>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    var appId = '{$signPackage.appId}';
    var timestamp = '{$signPackage.timestamp}';
    var nonceStr = '{$signPackage.nonceStr}';
    var signature = '{$signPackage.signature}';
    var userid = '{$userid}';
    $("#title").change(function (e){
        share();
    });
    $("#desc").change(function (e) {
        share();
    });
    $("#upload").change(function (e) {
        var file = e.target.files[0];
        var formData = new FormData();
        formData.append("file",file)
        $.ajax({
            url: "api/upload.php",
            type: "POST",
            processData: false,
            contentType: false,
            data: formData,
            success: function(data) {
                data = JSON.parse(data);
                if(data.file_path){
                    $("#img").attr("src", data.file_path);
                    $('#images').val(data.file_path);
                    share();
                }else{
                    alert(data);
                }
            }
        });

    });

    function share(a){
        wx.config({
            debug: false,
            appId: appId,
            timestamp:timestamp,
            nonceStr: nonceStr,
            signature: signature,
            jsApiList: [
                // 所有要调用的 API 都要加到这个列表中
                'onMenuShareTimeline',
                'onMenuShareAppMessage'
            ]
        });
        wx.ready(function(){
            var title = $('#title').val() ;
            var desc = $('#desc').val() ;
            var imgUrl = $('#images').val();
            if(!imgUrl){
                imgUrl = "http://dingwei.famishare.me/temp/default/image/hongbao.png";
            }else{
                imgUrl = "http://dingwei.famishare.me/"+imgUrl;
            }
            wx.onMenuShareTimeline({
                title:title, // 分享标题
                link: 'http://dingwei.famishare.me/panel.php?userid='+userid, // 分享链接
                imgUrl:imgUrl, // 分享图标
                desc:desc,
                success: function () {
                    // 用户确认分享后执行的回调函数
                    //  alert("分享成功");
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            wx.onMenuShareAppMessage({
                title: title,
                desc:desc,
                link: 'http://dingwei.famishare.me/panel.php?userid='+userid, // 分享链接
                imgUrl: imgUrl, // 分享图标
                type: '', // 分享类型,music、video或link，不填默认为link
                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                success: function () {
                    // 用户确认分享后执行的回调函数
                    //  alert("分享成功");
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
        })
    }

    </script>
</html>